package com.core.page;

/**
 * 分页器
 * 
 * @author pt-chenqw
 *
 */
public class Pager {

	/** 默认的分页大小 */
	public static final int DEFAULT_PAGE_SIZE = 10;

	/** 总的记录数 */
	private int total;

	/** 总的页数 */
	private int totalPage;

	/** 每页记录数 */
	private int pageSize;

	/** 当前页数 */
	private int currentPage;

	public Pager() {
	}

	public Pager(int total, int pageSize, int curPage) {
		create(total, pageSize, curPage);
	}

	private void create(int total, int pageSize, int curPage) {
		this.total = total;
		totalPage = 0;
		this.pageSize = pageSize;
		this.currentPage = curPage;
		count();
	}

	/**
	 * 计算总页数,当前页
	 */
	private void count() {
		if (pageSize <= 0)
			pageSize = DEFAULT_PAGE_SIZE;
		totalPage = total / pageSize;
		if (total % pageSize > 0)
			totalPage++;
		if (currentPage <= 0)
			currentPage = 1;
		if (currentPage > totalPage)
			currentPage = totalPage;
	}

	/**
	 * 总记录数
	 * 
	 * @return
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * 当前页
	 * 
	 * @return
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int curPage) {
		this.currentPage = curPage;
		count();
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		count();
	}

	public void setTotal(int total) {
		this.total = total;
		count();
	}

	/**
	 * 得到开始的记录
	 * 
	 * @return
	 */
	public int getStart() {
		return (currentPage > 1) ? (currentPage - 1) * pageSize : 0;
	}

	public int getRowIndex() {
		return getStart() + 1;
	}

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 每页记录数
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPage() {
		return currentPage + 1 <= totalPage ? currentPage + 1 : totalPage;
	}

	/**
	 * 前一页
	 * 
	 * @return
	 */
	public int getPrevPage() {
		return currentPage == 1 ? 1 : currentPage - 1;
	}

	/**
	 * 是否有下一页
	 * 
	 * @return
	 */
	public boolean hasNextPage() {
		return totalPage > currentPage;
	}

	/**
	 * 是否有上一页
	 * 
	 * @return
	 */
	public boolean hasPrevPage() {
		return currentPage > 1;
	}

	/**
	 * 是否最后一页
	 * 
	 * @return
	 */
	public boolean isLastPage() {
		return !hasNextPage();
	}
	
}
